/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.BusinessLogicLayer;

import edu.nii.qlks.DataAccessLayer.MSSQLConnectUnit;
import edu.nii.qlks.DataTransferObject.BCDoanhThuInfo;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author MARS_NHO
 */
public class BCDoanhThuBLL {
    MSSQLConnectUnit _connect;
    ResultSet  _result;
//  Hàm lấy danh sách theo điều kiện, sắp xếp
    
    public ArrayList LayDS(String Condition, String OderBy) throws Exception{
        ArrayList vt = new ArrayList();
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("BCDOANHTHU", Condition, OderBy);
        
        while (_result.next()){
            ArrayList vt_row = new ArrayList();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public ArrayList LayDS(String Condition) throws Exception{
         return LayDS(Condition, null);
    }
    
    public ArrayList LayDS() throws Exception{
        return LayDS(null);
    }
    
    public void Insert(BCDoanhThuInfo baoCaoDoanhThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("MaBaoCao", baoCaoDoanhThuInfo.getMaBaoCaoDoanhThu());
        map.put("ThangBaoCao", baoCaoDoanhThuInfo.getThangBaoCao());
        map.put("NamBaoCao", baoCaoDoanhThuInfo.getNamBaoCao());
        _connect.Insert("BCDOANHTHU", map);
    }
    
    public void Inserts(HashSet<BCDoanhThuInfo> baoCaoDoanhThuInfos) throws Exception{
        for (BCDoanhThuInfo baoCaoDoanhThuInfo:baoCaoDoanhThuInfos) {
            this.Insert(baoCaoDoanhThuInfo);
        }
    }
    
    public void Update(BCDoanhThuInfo baoCaoDoanhThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("ThangBaoCao", baoCaoDoanhThuInfo.getThangBaoCao());
        map.put("NamBaoCao", baoCaoDoanhThuInfo.getNamBaoCao());
        _connect.Update("BCDOANHTHU", map, "MaBaoCao = '" + baoCaoDoanhThuInfo.getMaBaoCaoDoanhThu() + "'");
    }
    
    public void Updates(HashSet<BCDoanhThuInfo> baoCaoDoanhThuInfos) throws Exception{
        for (BCDoanhThuInfo baoCaoDoanhThuInfo:baoCaoDoanhThuInfos){
            this.Update(baoCaoDoanhThuInfo);
        }
    }
    
    public void Delete(BCDoanhThuInfo baoCaoDoanhThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _connect.Delete("BCDOANHTHU", "MaBaoCao = '" + baoCaoDoanhThuInfo.getMaBaoCaoDoanhThu() + "'");
    }
    
    public void Deletes(HashSet<BCDoanhThuInfo> baoCaoDoanhThuInfos) throws Exception{
         for (BCDoanhThuInfo baoCaoDoanhThuInfo:baoCaoDoanhThuInfos){
            this.Delete(baoCaoDoanhThuInfo);
        }
    }
    
    public String TaoMaTuDong(String maGoc, int batDau){
        String ma = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.TaoMaTuDong(maGoc, batDau);
        return ma;
    }
    
    public ArrayList LayDS_MaLoaiPhong(String MaLoaiPhong, int ThangBC, int NamBC) throws Exception{
        ArrayList vt = new ArrayList();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("LOAIPHONG.MaLoaiPhong, LOAIPHONG.TenLoaiPhong, PHIEUTHUEPHONG.ThanhTien",
                "LOAIPHONG, PHONG, PHIEUTHUEPHONG, HOADON",
                " LOAIPHONG.MaLoaiPhong = '" + MaLoaiPhong + "' " + 
                " and LOAIPHONG.MaLoaiPhong = PHONG.MaLoaiPhong and PHONG.MaPhong = PHIEUTHUEPHONG.MaPhong " + 
                " and PHIEUTHUEPHONG.MaHoaDon = HOADON.MaHoaDon " +
                " and DatePart(Month,HOADON.NgayThanhToan) = " + ThangBC + " " + 
                " and DatePart(Year,HOADON.NgayThanhToan) = " + NamBC + " ", null);
        
        while (_result.next()){
            ArrayList vt_row = new ArrayList();
            
            String str = _result.getString(1);
            System.out.println("...."+str);
            vt_row.add(str);
            
            str = _result.getString(2);
            vt_row.add(str);
            System.out.println("...."+str);
            
            str = _result.getString(3);
            vt_row.add(str);
            System.out.println("...."+str);
            
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
}
